<script setup lang="ts">
import { onMounted, onUnmounted, ref, shallowRef } from 'vue'
import {
  AxesHelper,
  Scene,
  PerspectiveCamera,
  WebGLRenderer,
  MeshStandardMaterial,
  Mesh,
  PlaneGeometry,
  MeshBasicMaterial,
  ShaderMaterial,
  RawShaderMaterial,
  DoubleSide,
  Clock,
  TextureLoader,
} from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { GUI } from 'dat.gui'
import Stats from 'three/examples/jsm/libs/stats.module'
import planeFrag from './plane.frag?raw'
import planeVert from './plane.vert?raw'

const contentRef = ref<HTMLCanvasElement>()
const renderer = shallowRef<WebGLRenderer>(new WebGLRenderer())
const orbitControls = shallowRef<OrbitControls>()
const scene = new Scene()
const camera = shallowRef<PerspectiveCamera>()
const axesHelper = new AxesHelper(20)
const clock = new Clock()
// 创建性能监视器
let stats = new Stats()

const guiParams = {
  uScale: 0.6,
  uNumber: 0.6,
  uNoiseFrequency: 0.6,
  uNoiseSize: 1.5,
}

const planeShaderMaterial = new ShaderMaterial({
  vertexShader: planeVert,
  fragmentShader: planeFrag,
  uniforms: {
    uElapsedTime: {
      value: 0,
    },
    uScale: {
      value: guiParams.uScale,
    },
    uNumber: {
      value: guiParams.uNumber,
    },
    uNoiseFrequency: {
      value: guiParams.uNoiseFrequency,
    },
    uNoiseSize: {
      value: guiParams.uNoiseSize,
    },
  },
  // wireframe: true,
  side: DoubleSide,
})

const gui = new GUI()

const init = () => {
  if (contentRef.value) {
    camera.value = new PerspectiveCamera(75, contentRef.value.clientWidth / contentRef.value.clientHeight)
    camera.value.position.set(0, 10, 20)
    scene.add(camera.value)
    scene.add(axesHelper)
    contentRef.value.appendChild(renderer.value.domElement)

    stats.dom.style.position = 'absolute'
    stats.dom.style.top = '0'
    stats.dom.style.left = '0'
    contentRef.value.appendChild(stats.dom)

    renderer.value.setSize(contentRef.value?.clientWidth, contentRef.value?.clientHeight)

    orbitControls.value = new OrbitControls(camera.value, renderer.value.domElement)
    orbitControls.value.enableDamping = true
  }
}

const render = () => {
  const elapsedTime = clock.getElapsedTime()
  planeShaderMaterial.uniforms.uElapsedTime.value = elapsedTime

  if (camera.value) {
    orbitControls.value?.update()
    renderer.value?.render(scene, camera.value)
  }
  stats.update()
  requestAnimationFrame(render)
}

window.addEventListener('resize', () => {
  if (contentRef.value && camera.value) {
    camera.value.aspect = contentRef.value.clientWidth / contentRef.value.clientHeight
    camera.value.updateProjectionMatrix()

    renderer.value.setSize(contentRef.value?.clientWidth, contentRef.value?.clientHeight)
    renderer.value.setPixelRatio(window.devicePixelRatio)
  }
})

onMounted(() => {
  init()

  const planeGeometry = new PlaneGeometry(20, 20, 1024, 1024)
  const planeMesh = new Mesh(planeGeometry, planeShaderMaterial)
  planeMesh.rotateX(-Math.PI / 2)

  scene.add(planeMesh)

  gui.add(guiParams, 'uScale', 0, 10, 0.01).onChange((v) => {
    planeShaderMaterial.uniforms.uScale.value = v
  })
  gui.add(guiParams, 'uNumber', 0, 10, 0.01).onChange((v) => {
    planeShaderMaterial.uniforms.uNumber.value = v
  })
  gui.add(guiParams, 'uNoiseFrequency', 0, 10, 0.01).onChange((v) => {
    planeShaderMaterial.uniforms.uNoiseFrequency.value = v
  })
  gui.add(guiParams, 'uNoiseSize', 0, 10, 0.01).onChange((v) => {
    planeShaderMaterial.uniforms.uNoiseSize.value = v
  })

  render()
})

onUnmounted(() => {
  gui.destroy()
})
</script>

<template>
  <div class="shader-water" ref="contentRef"></div>
</template>

<style lang="scss" scoped>
.shader-water {
  width: 100%;
  height: 100%;
  position: relative;
}
</style>
